package com.web.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class DateFormatUtils {
	
	public static final String DATE_FORMAT_1 = "yyyy-MM-dd";
	public static final String DATE_FORMAT_2 = "yyyy-MM-dd HH:mm:ss";

	/**
	 * 根据指定时间和指定格式获取格式化日期
	 * 
	 * @param time
	 * @param format
	 * @return
	 */
	public static String getDateByTime(long time, String format) {
		Date date = new Date(time);
		SimpleDateFormat sDateformat = new SimpleDateFormat(format);
		return sDateformat.format(date).toString();
	}

	/**
	 * 日期String类型转化为long类型
	 * 
	 * @param dateStr
	 * @param format
	 * @return
	 */
	public static long getTimeByDateStr(String dateStr, String format) {
		if (dateStr == null || "".equals(dateStr) || null == format
				|| "".equals(format)) {
			return 0L;
		}
		SimpleDateFormat f = new SimpleDateFormat(format);
		Date date = null;
		try {
			date = f.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		long rtn;
		if (date == null) {
			rtn = 0L;
		} else {
			rtn = date.getTime();
		}
		return rtn;
	}
	
	
	/**
	 * 获取Calendar实例
	 * 
	 * @return Calendar
	 */
	public static Calendar getCalendar() {
		Calendar calendar = Calendar.getInstance(TimeZone
				.getTimeZone("GMT+8:00"));
		calendar.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
		// calendar.get(Calendar.HOUR_OF_DAY);
		// calendar.set(Calendar.HOUR, Calendar.HOUR_OF_DAY);
		return calendar;
	}

	/**
	 * 获取当前年份
	 * @return
	 */
	public static int getCurrentYear(){
		return getCalendar().get(Calendar.YEAR);
	}
	
	/**
	 * 获取当前月份
	 * @return
	 */
	public static int getCurrentMonth(){
		return getCalendar().get(Calendar.MONTH)+1;
	}

	/**
	 * 获取当前天份
	 * @return
	 */
	public static int getCurrentDate(){
		return getCalendar().get(Calendar.DATE);
	}

}
